<?php

class ProfilController extends FBController
{

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
		 */
	public function accessRules()
	{
		return array(
		);
	}

	public function actionIndex()
	{
	
      	$model = Yii::app()->db->createCommand()
    		->select()
    		->from('{{profil}}')
    		->where('uid="'.$this->FBUser.'"')
    		->queryRow();

		$this->render('index', array(
			'model'=>$model,
			'profile'=>$this->FBUser_profile, 
			));
	}
	/**
	 * Displays a particular model.
	 */
	public function actionUpdate($id)
	{
		$profil = Profil::model()->findByPk($id);
		if(isset($_POST['Profil']))
		{
			$profil->attributes=$_POST['Profil'];
			$profil->town = $_POST['Profil']['town'];
			$profil->street = $_POST['Profil']['street'];
			$profil->distance = $_POST['Profil']['distance'];
			
			$coord=get_coordinates($_POST['Profil']['town'].','.$_POST['Profil']['street']);

				$profil->lat = $coord['latitude'];
				$profil->lon = $coord['longitude'];
			
			$profil->save();
			$this->redirect(array('index'));
		}
    		
		$this->render('update', array(
			'profil'=>$profil,
			'fbprofile'=>$this->FBUser_profile, 
		));
	}
	
	/**
	 * Update Profileinträge
	 */
	public function actionView($id)
	{
		Yii::log('Index', 'info', 'appl');
		$now=new CDbExpression("NOW()");
		$model=Yii::app()->db->createCommand()
    		->select()
    		->from('{{news}} n')
    		->where('n.id=:id',array(':id'=>$id))
    		->queryRow();
    		
		$this->render('view', array(
			'model'=>$model,
		));
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
			{
				//if(Yii::app()->user->isGuest)
					//$condition='status='.Post::STATUS_PUBLISHED.' OR status='.Post::STATUS_ARCHIVED;
				//else
				//	$condition='';
				$this->_model=Page::model()->findByPk($_GET['id'], $condition);
			}
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model; //$this->_model;
	}

	/**
	 * Creates a new comment.
	 * This method attempts to create a new comment based on the user input.
	 * If the comment is successfully created, the browser will be redirected
	 * to show the created comment.
	 * @param Post the post that the new comment belongs to
	 * @return Comment the comment instance
		 */
	protected function newComment($model)
	{
		$comment=new Comment;
		$page = Page::model()->findByPk($model->id);
		if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
		{
			echo CActiveForm::validate($comment);
			Yii::app()->end();
		}
		if(isset($_POST['Comment']))
		{
			$comment->attributes=$_POST['Comment'];
			if($page->addComment($comment))
			{
				//if($comment->status==Comment::STATUS_PENDING)
				//	Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
				$this->refresh();
			}
		}
		return $comment;
	}

}
